package com.cuitrip.business.home.recommend.ui;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuitrip.apiservice.TrackBusiness;
import com.cuitrip.business.home.recommend.RecommendCardListFragment;
import com.cuitrip.business.home.recommend.model.HomeCard;
import com.cuitrip.business.track.TrackProxy;
import com.cuitrip.service.R;
import com.lab.adapter.BaseHolderView;
import com.lab.adapter.IAdapterData;
import com.lab.schemeurl.SchemeUrlHandler;
import com.lab.schemeurl.constant.Host;
import com.lab.utils.ImageHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.ButterKnife;
import butterknife.Bind;


/**
 * 首页卡片的holderview
 * Created by ak
 */
public class CardItemHolderView extends BaseHolderView {

    @Bind(R.id.card_title)
    TextView titleView;
    @Bind(R.id.card_sub_title)
    TextView subTitleView;
    @Bind(R.id.card_img)
    ImageView cardImageView;

    public CardItemHolderView(Context context) {
        super(context, R.layout.item_card);
    }

    @Override
    public void initView(View rootView) {
        this.setClickable(true);
        ButterKnife.bind(this);
    }

    private List<HomeCard> cardList = new ArrayList<>();

    public void setCurrentList(List<HomeCard> cards){
        this.cardList = cards;
    }

    private int page = 0;
    public void setPage(int page){
        this.page = page;
    }

    @Override
    public void bindData(final IAdapterData data, final int position) {
        if (data instanceof HomeCard) {
            final HomeCard card = (HomeCard) data;
            ImageHelper.displayCtImage(((HomeCard) data).getBackPic(), cardImageView, ImageHelper.getCardFiveFourImageOptions());
            titleView.setText(card.getTitle());
            subTitleView.setText(card.getSubTitle());
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(card.getUrl())) {
                        StringBuilder urlBuilder = new StringBuilder(card.getUrl());
                        if (SchemeUrlHandler.getInstance().isAppScheme(card.getUrl())) {
                            if (Host.TRIP.getHostName().equals(Uri.parse(card.getUrl()).getHost().trim())
                                    || Host.RECOMMEND.getHostName().equals(Uri.parse(card.getUrl()).getHost().trim())
                                    || Host.SEARCH.getHostName().equals(Uri.parse(card.getUrl()).getHost().trim())) {
                                Set<String> paras = Uri.parse(card.getUrl()).getQueryParameterNames();
                                if (paras.size() == 0){
                                    urlBuilder.append("?");
                                    urlBuilder.append("from=app_card");
                                }else{
                                    urlBuilder.append("&from=app_card");
                                }
                                urlBuilder.append("&title=").append( card.getTitle());
                            }
                            urlBuilder.append("&topicType=").append(card.getTopicType())
                                    .append("&type=").append(card.getType());
                            if (Host.SEARCH.getHostName().equals(Uri.parse(card.getUrl()).getHost().trim())){
                                if (card.searchParam != null) {
                                        urlBuilder.append("&searchParam=").append(card.searchParam);
                                }
                            }
                        }
                        SchemeUrlHandler.getInstance().dispatch(mContext, Uri.parse(urlBuilder.toString()));
                        List<String> ids = new ArrayList<String>();
                        for (HomeCard trip : cardList) {
                            ids.add(String.valueOf(trip.getId()));
                        }
                        TrackProxy.getInstance().operationAction(TrackBusiness.Source.RECOMMEND, TrackBusiness.Action.CLICK, String.valueOf(page), TrackProxy.getInstance().getCurrentPageIndex(RecommendCardListFragment.PAGE_SIZE, position), ids);
                    }
                }
            });
        }
    }


}
